package com.haixiaoke.flat.user.controller;

import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.auth.ali.service.AliAuthService;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.user.domain.CheckInPerson;
import com.haixiaoke.saas.user.service.ICheckInPersonService;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 入住人管理Controller
 *
 * @author panda
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/front/user/person")
public class FlatCheckInPersonController extends BaseController {
    @Autowired
    private ICheckInPersonService checkInPersonService;
    @Autowired
    private AliAuthService aliAuthService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 查询入住人管理列表
     */
    @AuthCheck
    @GetMapping("/list")
    public AjaxResult list(CheckInPerson checkInPerson) {
        checkInPerson.setUserId(frontTokenComponent.getUserId());
        List<CheckInPerson> list = checkInPersonService.selectCheckInPersonList(checkInPerson);
        return success(list);
    }

    /**
     * 获取入住人管理详细信息
     */
    @AuthCheck
    @GetMapping(value = "/{personId}")
    public AjaxResult getInfo(@PathVariable("personId") Long personId) {
        return success(checkInPersonService.selectCheckInPersonByPersonId(personId));
    }

    /**
     * 新增入住人管理
     */
    @AuthCheck
    @PostMapping
    public AjaxResult add(@RequestBody CheckInPerson checkInPerson) {
        if (StrUtil.isBlank(checkInPerson.getPersonPhone()) || StrUtil.isBlank(checkInPerson.getPersonName())
                || StrUtil.isBlank(checkInPerson.getIdCard())) {
            return error("请填写完整信息");
        }
        if (!Pattern.compile("^1[3-9]\\d{9}$").matcher(checkInPerson.getPersonPhone()).matches()) {
            return error("手机号格式不正确");
        }
        checkInPerson.setUserId(frontTokenComponent.getUserId());
        if (checkInPersonService.checkPersonByPhone(checkInPerson.getPersonId(),checkInPerson.getUserId(),checkInPerson.getPersonPhone())){
            return error("该手机号已被使用");
        }
        CheckInPerson personByIdCard = checkInPersonService.selectPersonByIdCard(checkInPerson.getIdCard(),checkInPerson.getUserId());
        if (ObjectUtil.isNotEmpty(personByIdCard)) {
            return error("该身份证信息已添加");
        }
        Boolean flag = aliAuthService.identityCheck(checkInPerson.getPersonName(),checkInPerson.getIdCard());
        if (!flag) {
            return error("身份证号与姓名不匹配");
        }
        checkInPerson.setCreateBy(frontTokenComponent.getLoginUser().getUser().getUserName());
        return toAjax(checkInPersonService.insertCheckInPerson(checkInPerson));
    }

    /**
     * 修改入住人管理
     */
    @AuthCheck
    @PutMapping
    public AjaxResult edit(@RequestBody CheckInPerson checkInPerson) {
        if (StrUtil.isBlank(checkInPerson.getPersonPhone())) {
            return error("请填写完整信息");
        }
        if (!Pattern.compile("^1[3-9]\\d{9}$").matcher(checkInPerson.getPersonPhone()).matches()) {
            return error("手机号格式不正确");
        }
        if (!Objects.equals(frontTokenComponent.getUserId(), checkInPerson.getUserId())) {
            return error("操作的数据异常");
        }
        if (checkInPersonService.checkPersonByPhone(checkInPerson.getPersonId(),checkInPerson.getUserId(),checkInPerson.getPersonPhone())){
            return error("该手机号已被使用");
        }
        checkInPerson.setUpdateBy(frontTokenComponent.getLoginUser().getUser().getUserName());
        return toAjax(checkInPersonService.updateCheckInPerson(checkInPerson));
    }

    /**
     * 删除入住人管理
     */
    @Log(title = "入住人管理", businessType = BusinessType.DELETE)
    @AuthCheck
    @DeleteMapping("/{personIds}")
    public AjaxResult remove(@PathVariable Long[] personIds) {
        return toAjax(checkInPersonService.softDeleteCheckInPersonByPersonIds(personIds));
    }
}
